import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { MODEL_STATE } from '../../constants';

export interface GeneralState {
  updates: number;
  editMode: boolean;
  modelState: string;
  modelPath: string | null;
  dialogOpen: boolean;
  dialogTitle: string;
  dialogMessage: string;
  confirmationDialogTitle: string;
  confirmationDialogMessage: string;
  confirmationDialogOnConfirm: Record<string, any>;
  automaticSimulation: boolean;
  automaticInstantiation: boolean;
  confirmationDialogOpen: boolean;
  notebookOpened: boolean;
  theme: string;
  instances: any[];
}

const initialState: GeneralState = {
  updates: 0,
  editMode: true,
  modelState: MODEL_STATE.NOT_INSTANTIATED,
  modelPath: null,
  dialogOpen: false,
  dialogTitle: '',
  dialogMessage: '',
  confirmationDialogTitle: '',
  confirmationDialogMessage: '',
  confirmationDialogOnConfirm: {},
  automaticSimulation: false,
  automaticInstantiation: false,
  confirmationDialogOpen: false,
  notebookOpened: false,
  theme: 'gui',
  instances: [],
};

const generalSlice = createSlice({
  name: 'general',
  initialState,
  reducers: {
    updateCards: (state) => {
      state.updates += 1;
    },
    showNetwork: (state) => {
      state.editMode = false;
    },
    createNetwork: (state) => {
      state.editMode = false;
      state.modelState = MODEL_STATE.INSTANTIATED;
    },
    createAndSimulateNetwork: (state) => {
      state.editMode = false;
      state.modelState = MODEL_STATE.SIMULATED;
    },
    simulateNetwork: (state, action: PayloadAction<boolean | undefined>) => {
      state.editMode = false;
      state.modelState = MODEL_STATE.SIMULATED;
    },
    registerModelPath: (state, action: PayloadAction<string>) => {
      state.modelPath = action.payload;
    },
    editModel: (state) => {
      state.editMode = true;
      state.updates += 1;
    },
    resetModel: () => initialState,
    openDialog: (state, action: PayloadAction<{ title: string; message: string }>) => {
      state.dialogOpen = true;
      state.dialogTitle = action.payload.title;
      state.dialogMessage = action.payload.message;
    },
    closeDialog: (state) => {
      state.dialogOpen = false;
    },
    openConfirmationDialog: (state, action: PayloadAction<{
      title: string;
      message: string;
      onConfirm: Record<string, any>;
    }>) => {
      state.confirmationDialogOpen = true;
      state.confirmationDialogTitle = action.payload.title;
      state.confirmationDialogMessage = action.payload.message;
      state.confirmationDialogOnConfirm = action.payload.onConfirm;
    },
    closeConfirmationDialog: (state) => {
      state.confirmationDialogOpen = false;
      state.confirmationDialogTitle = '';
      state.confirmationDialogMessage = '';
      state.confirmationDialogOnConfirm = {};
    },
    setAutomaticInstantiation: (state, action: PayloadAction<boolean>) => {
      state.automaticInstantiation = action.payload;
      state.automaticSimulation = state.automaticSimulation && action.payload;
    },
    setAutomaticSimulation: (state, action: PayloadAction<boolean>) => {
      state.automaticSimulation = action.payload;
      state.automaticInstantiation = action.payload || state.automaticInstantiation;
    },
    setTheme: (state, action: PayloadAction<string>) => {
      state.theme = action.payload;
    },
    loadTutorial: (state) => {
      state.updates += 1;
    },
    addCanvasInstances: (state, action: PayloadAction<any[]>) => {
      // Implementation for adding instances
    },
    changeInstanceColor: (state, action: PayloadAction<{ instancePath: string; color: any }>) => {
      // Implementation for changing instance color
    },
    removeCanvasInstances: (state, action: PayloadAction<string[]>) => {
      // Implementation for removing instances
    },
    selectInstances: (state, action: PayloadAction<{ instances: any[]; selectedInstances: string[] }>) => {
      // Implementation for selecting instances
    },
  },
});

export const {
  updateCards,
  showNetwork,
  createNetwork,
  createAndSimulateNetwork,
  simulateNetwork,
  registerModelPath,
  editModel,
  resetModel,
  openDialog,
  closeDialog,
  openConfirmationDialog,
  closeConfirmationDialog,
  setAutomaticInstantiation,
  setAutomaticSimulation,
  setTheme,
  loadTutorial,
  addCanvasInstances,
  changeInstanceColor,
  removeCanvasInstances,
  selectInstances,
} = generalSlice.actions;

// Action creator for Python calls (handled by middleware)
export const pythonCall = (cmd: string, args?: Record<string, any>) => ({
  type: 'PYTHON_CALL',
  cmd,
  args,
});

export default generalSlice.reducer;

